package com.margo.project.socket.post;

import java.io.File;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.FileEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;


/**
 * 用httpClient发送请求
 * @author fangxingbang
 *
 */
public class HttpClientPostClient {
	public static void main(String[] args) {
		fileEntry();
		//encodedFormEntity();
	}
	
	/**
	 * UrlEncodedFormEntity
	 * http报文为:
	 * 
	 * POST /iep/mobile/mobileservice/fileAction/upByteFile.iep HTTP/1.1
	 * Content-Length: 12
	 * Content-Type: application/x-www-form-urlencoded; charset=UTF-8
	 * Host: localhost:8080
	 * Connection: Keep-Alive
	 * User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_171)
	 * Accept-Encoding: gzip,deflate
	 * 
	 * method=margo
	 * 
	 */
	static void encodedFormEntity() {
		try {
			String url = "http://localhost:8080/iep/mobile/mobileservice/fileAction/upByteFile.iep";
			CloseableHttpClient httpclient = HttpClients.createDefault();
			// 
			HttpPost httpPost = new HttpPost(url);
			List<BasicNameValuePair> params=new ArrayList<BasicNameValuePair>();
			params.add(new BasicNameValuePair("method", "margo"));
			httpPost.setEntity(new UrlEncodedFormEntity(params,"UTF-8"));
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println(":" + state + "(" + url + ")");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * StringEntity
	 * http报文为:
	 * 
	 * POST /iep/mobile/mobileservice/fileAction/upByteFile.iep HTTP/1.1
	 * Content-Length: 5
	 * Content-Type: text/plain; charset=UTF-8
	 * Host: localhost:8080
	 * Connection: Keep-Alive
	 * User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_171)
	 * Accept-Encoding: gzip,deflate
	 * 
	 * margo
	 * 
	 */
	static void stringEntry() {
		try {
			String url = "http://localhost:8080/iep/mobile/mobileservice/fileAction/upByteFile.iep";
			CloseableHttpClient httpclient = HttpClients.createDefault();
			// 
			HttpPost httpPost = new HttpPost(url);	
			StringEntity stringEntity = new StringEntity("margo","UTF-8");
			httpPost.setEntity(stringEntity);
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println(":" + state + "(" + url + ")");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	/**
	 * FileEntity
	 * 报文为：
	 * 
	 * POST /iep/mobile/mobileservice/fileAction/upByteFile.iep HTTP/1.1
	 * Content-Length: 5
	 * Host: localhost:8080
	 * Connection: Keep-Alive
	 * User-Agent: Apache-HttpClient/4.5.3 (Java/1.8.0_151)
	 * Accept-Encoding: gzip,deflate
	 * 
	 * margo
	 */
	static void fileEntry() {
		try {
			String url = "http://localhost:8080/iep/mobile/mobileservice/fileAction/upByteFile.iep";
			CloseableHttpClient httpclient = HttpClients.createDefault();
			// 
			HttpPost httpPost = new HttpPost(url);	
			URI uri = HttpClientPostClient.class.getResource("/file/socket.txt").toURI();
			File file=new File(uri);
			FileEntity fileEntity = new FileEntity(file);
			httpPost.setEntity(fileEntity);
			HttpResponse response = httpclient.execute(httpPost);
			StatusLine status = response.getStatusLine();
			int state = status.getStatusCode();
			if (state == HttpStatus.SC_OK) {
				HttpEntity responseEntity = response.getEntity();
				String jsonString = EntityUtils.toString(responseEntity, "UTF-8");
				System.out.println(jsonString);
			} else {
				System.out.println(":" + state + "(" + url + ")");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}	
	}
	
	
}
